#!/usr/bin/env python
#--*-- coding:utf-8 --*--

import sys
import os
import os.path
import platform
from pyqt4_pyside import *

from appconf import appconf
from settingspage import SettingsPage
from calcpage import CalcPage
from calcpage2 import CalcPage2
from helppage import HelpPage

class Dialog(QtGui.QDialog):
    def __init__(self, parent=None):
        super(Dialog, self).__init__(parent)
        vbox = QtGui.QVBoxLayout(self)
        vbox.setContentsMargins(0, 0, 0, 0)

        self.stackedwidget = QtGui.QStackedWidget()
        self.calcPage = CalcPage()
        self.calcPage2 = CalcPage2()
        self.settingsPage = SettingsPage()
        self.helpPage = HelpPage()
        self.stackedwidget.addWidget(self.calcPage)
        self.stackedwidget.addWidget(self.calcPage2)
        self.stackedwidget.addWidget(self.settingsPage)
        self.stackedwidget.addWidget(self.helpPage)
        self.toolbar = QtGui.QToolBar()
        vbox.addWidget(self.toolbar)
        vbox.addWidget(self.stackedwidget)
        self.readSettings()
        self.createMainToolBar()

        self.setWindowTitle(u"小立计算")
        
    def createMainToolBar(self):
        self.toolbar.setToolButtonStyle(QtCore.Qt.ToolButtonTextUnderIcon)
        self.actionsGroup = QtGui.QActionGroup(self)
        self.calcToolAction = self.createMainToolAction(u"计算",  QtGui.QIcon(QtGui.QPixmap(40, 40)))
        self.calcToolAction.setChecked(True)
        self.calc2ToolAction = self.createMainToolAction(u"计算2",  QtGui.QIcon(QtGui.QPixmap(40, 40)))
        self.settingsToolAction = self.createMainToolAction(u"设置",  QtGui.QIcon(QtGui.QPixmap(40, 40)))
        self.toolbar.addSeparator()
        self.helpToolAction = self.createMainToolAction(u"帮助",  QtGui.QIcon(QtGui.QPixmap(40, 40)))
        self.actionsGroup.triggered.connect(self.switchPages)
        self.calc2ToolAction.trigger()
        
    def createMainToolAction(self,  text,  icon=QtGui.QIcon()):
        act = QtGui.QAction(icon,  text,  self.actionsGroup)
        act.setCheckable(True)
        self.toolbar.addAction(act)
        return act
    
    def switchPages(self,  act):
        if act == self.calcToolAction:
            self.stackedwidget.setCurrentIndex(0)
        elif act == self.calc2ToolAction:
            self.stackedwidget.setCurrentIndex(1)
        elif act == self.settingsToolAction:
            self.stackedwidget.setCurrentIndex(2)
        elif act == self.helpToolAction:
            self.stackedwidget.setCurrentIndex(3)

    def closeEvent(self,  evt):
        self.writeSettings()
        self.settingsPage.writeSettings()
        self.calcPage.writeSettings()
        self.calcPage2.writeSettings()
        
    def readSettings(self):
        settings = QtCore.QSettings()
        settings.beginGroup("window")
        self.resize(settings.value("size",  QtCore.QSize(600, 400)))
        settings.endGroup()
        
    def writeSettings(self):
        settings=QtCore.QSettings()
        settings.beginGroup("window")
        settings.setValue("size",  self.size())
        settings.endGroup()

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    app.setOrganizationName(appconf['orgShortName'])
    app.setOrganizationDomain(appconf['domainName'])
    app.setApplicationName(appconf['appName'])
    app.setApplicationVersion(appconf['appVersion'])
    QtCore.QSettings.setDefaultFormat(QtCore.QSettings.IniFormat)
    w = Dialog()
    w.show()
    sys.exit(app.exec_())
